﻿using DevExpress.XtraEditors;
using MesProjectWinform.BasicData.Entity;
using MesProjectWinform.Production.Common;
using MesProjectWinform.Production.Dto.Input.WorkOrder;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using MesProjectWinform.Public.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.WorkOrder
{
    public partial class SplitForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void MyDelegate(); // 定义委托类型
        public MyDelegate MyDelegateCallback; // 定义委托实例
        public SplitForm()
        {
            InitializeComponent();
        }

        public void GetData(string code,int num,object productionlineid)
        {
            
            textEdit1.Text = code;
            textEdit2.Text = num.ToString();
            lookUpEdit1.EditValue = productionlineid;
        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private async void simpleButton2_Click(object sender, EventArgs e)
        {
            simpleButton2.Enabled= false; // 禁用按钮，防止重复提交
            SplitWorkOrderInput input = new SplitWorkOrderInput()
            {
                EndTime = dateEdit2.DateTime,
                StartTime = dateEdit1.DateTime,
                Productionlineid = lookUpEdit1.EditValue,
                SplitNum = (int)spinEdit1.Value,
                Remark = memoEdit1.Text,
                WorkOrderCode = textEdit1.Text,
            };
            var data = JsonConvert.SerializeObject(input);
            var response = await HttpClientHelper.RequestUrl(RequestType.Post, ProductionGolab.SplitProduct,data);
            var result = JsonConvert.DeserializeObject<ApiResult<int>>(response);
            if(result.Code==ApiEnums.Success)
            {
                this.Close(); // 关闭当前窗体
                MyDelegateCallback?.Invoke(); // 调用委托方法，通知父窗体刷新数据
                MessageBox.Show(result.Message);
            }
            else
            {
                simpleButton2.Enabled = true; // 启用按钮
                MessageBox.Show(result.Message);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void SplitForm_Load(object sender, EventArgs e)
        {
            // 假设有 gridView1 和常用按钮 simpleButton1, simpleButton2, simpleButton3
            // 表格美化（如有gridView1）
            var gridControl = this.Controls.Find("gridControl1", true).FirstOrDefault() as DevExpress.XtraGrid.GridControl;
            var gridView1 = gridControl?.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (gridView1 != null)
            {
                gridView1.OptionsView.EnableAppearanceEvenRow = true;
                gridView1.Appearance.EvenRow.BackColor = Color.FromArgb(245, 249, 255);
                gridView1.Appearance.HeaderPanel.Font = new Font("微软雅黑", 11, FontStyle.Bold);
                gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridView1.Appearance.Row.Font = new Font("微软雅黑", 10);
                gridView1.OptionsView.ShowGroupPanel = false;
                // 只对非复选框列设置只读
                foreach (DevExpress.XtraGrid.Columns.GridColumn col in gridView1.Columns)
                {
                    if (col.FieldName != "IsSelected" && col.FieldName != "选择")
                    {
                        col.OptionsColumn.AllowEdit = false;
                        col.OptionsColumn.ReadOnly = true;
                    }
                    else
                    {
                        col.OptionsColumn.AllowEdit = true;
                        col.OptionsColumn.ReadOnly = false;
                    }
                }
                gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
                gridView1.OptionsSelection.EnableAppearanceFocusedRow = true;
                gridView1.Appearance.FocusedRow.BackColor = Color.FromArgb(200, 230, 255);
                gridView1.Appearance.FocusedRow.ForeColor = Color.Black;
                gridView1.Appearance.SelectedRow.BackColor = Color.FromArgb(0, 120, 215);
                gridView1.Appearance.SelectedRow.ForeColor = Color.White;
                gridView1.Appearance.Row.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                gridView1.RowHeight = 32;
                gridView1.Appearance.Row.BackColor = Color.White;
                gridView1.Appearance.Row.BorderColor = Color.FromArgb(230, 230, 230);
                gridView1.OptionsView.ShowHorizontalLines = DevExpress.Utils.DefaultBoolean.False;
                gridView1.OptionsView.ShowVerticalLines = DevExpress.Utils.DefaultBoolean.False;
                gridView1.OptionsView.ShowIndicator = false;
            }
            // 按钮美化（如有）
            foreach (var btnName in new[] { "simpleButton1", "simpleButton2", "simpleButton3" })
            {
                var btn = this.Controls.Find(btnName, true).FirstOrDefault() as DevExpress.XtraEditors.SimpleButton;
                if (btn != null)
                {
                    btn.Appearance.Font = new Font("微软雅黑", 10, FontStyle.Bold);
                    btn.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.UltraFlat;
                    btn.Appearance.BackColor = Color.FromArgb(240, 240, 240);
                    btn.Appearance.ForeColor = Color.Black;
                }
            }
            #region 初始化产线下拉框
            List<Productionline> productionlines = new List<Productionline>();
            //这里可以从数据库获取数据，或者直接写死数据
            var response =await HttpClientHelper.RequestUrl(RequestType.Get, ProductionGolab.GetProductionLine, null);
            var plapiResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult<List<Productionline>>>(response);
            productionlines = plapiResult.Data;
            lookUpEdit1.Properties.DataSource = productionlines; // 绑定数据源
            lookUpEdit1.Properties.DisplayMember = "Productionlinename"; // 显示的字段
            lookUpEdit1.Properties.ValueMember = "Id"; // 实际存储的字段
            //设置下拉列表的列
            lookUpEdit1.Properties.Columns.Clear();
            lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Productionlinename", "产线"));
            //下拉框启动时默认显示请选择
            lookUpEdit1.Properties.NullText = "请选择产线";
            #endregion
        }
    }
}